package cola.rh.System.Controller;

import cola.rh.System.Entity.User;
import cola.rh.System.Entity.vo.UserQueryVo;
import cola.rh.System.Service.UserService;
import cola.rh.System.common.result.Result;
import cola.rh.System.exception.ColaException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name: UserController
 * @Author: Cola
 * @Time: 2023/3/1 13:59
 * @Description: UserController
 */
@Api(tags = "用户管理")
@RestController
@Slf4j
@RequestMapping("/v1/api/users")
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation("HelloSpringBoot")
    @GetMapping("/helloSpringBoot")
    public String helloSpringBoot() {
        return "HelloSpringBoot";
    }

    @ApiOperation("RH")
    @GetMapping("/rh")
    public String helloRH() {
        return "Hello RH";
    }

    @ApiOperation("CBQ")
    @GetMapping("/cbq")
    public String helloCBQ() {
        return "Hello CBQ";
    }


    /**
     * 获取全部用户列表
     *
     * @return Result<List < User>>
     */

    @ApiOperation("获取全部用户列表")
    @GetMapping("findAll")
    public Result<List<User>> findAllUser() {
        List<User> userList = userService.list();
        return Result.ok(userList);
    }

    /**
     * 条件分页查询
     *
     * @return Result
     */

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryUser(@PathVariable Long page,
                                @PathVariable Long limit,
                                UserQueryVo userQueryVo) {
        IPage<User> pageModel = userService.pageQueryUser(page, limit, userQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * Md5 加密 密码后保存用户
     *
     * @param user user
     * @return Result.ok
     */

    @ApiOperation(value = "新增用户")
    @PostMapping("save")
    public Result saveByMd5(@RequestBody User user) {
        userService.saveByMd5(user);
        return Result.ok();
    }

    /**
     * 更新用户状态
     *
     * @param id     id
     * @param status status  状态 （1 正常 | 0 封禁）
     * @return Result.ok
     */
    @ApiOperation(value = "更新用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status) {
        userService.updateStatus(id, status);
        return Result.ok();
    }

    /**
     * 根据 ID 获取用户
     *
     * @param id ID
     * @return User
     */

    @ApiOperation(value = "根据 ID 获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.ok(user);
    }


    /**
     * 修改用户
     *
     * @param user
     * @return Result.ok
     */


    @ApiOperation(value = "修改用户")
    @PutMapping("update")
    public Result updateById(@RequestBody User user) {
        userService.updateById(user);
        return Result.ok();
    }

    /**
     * 根据 ID 删除用户
     *
     * @param id ID
     * @return Result.ok
     */


    @ApiOperation(value = "根据 ID 删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        userService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据 IDS 列表删除用户
     *
     * @param idList IDS 列表
     * @return Result.ok
     */


    @ApiOperation(value = "根据 IDS 列表删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        userService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "ArithmeticException 异常处理器")
    @DeleteMapping("ArithmeticExceptionTest")
    public Result ArithmeticExceptionTest() {
        try {
            Integer x = 1 / 0;
        } catch (Exception e) {
            throw new ArithmeticException();
        }
        return Result.ok();
    }

    @ApiOperation(value = "ColaException 异常处理器")
    @DeleteMapping("ColaException")
    public Result ColaException() {
        try {
            Integer x = 1 / 0;
        } catch (Exception e) {
            throw new ColaException(201, "ColaException");
        }
        return Result.ok();
    }
}
